package com.example.quick_dev_plugin

import android.content.Intent
import android.util.Log
import androidx.annotation.NonNull
import com.blankj.utilcode.util.AppUtils

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler

/** QuickDevPlugin */
class QuickDevPlugin: FlutterPlugin, MethodCallHandler {
  private lateinit var channel : MethodChannel

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "quick_dev_plugin")
    channel.setMethodCallHandler(this)
  }

  override fun onMethodCall( call: MethodCall, result: MethodChannel.Result) {
    Log.e("SunJH", "flutter 调用")
    when (call.method) {
      "getPlatformVersion"->{
        result.success("Android ${android.os.Build.VERSION.RELEASE}")

      }
      "installApp" -> {
        val appFilePath = call.argument<String>("appFilePath")
        Log.e("SunJH", "appFilePath is $appFilePath")
        if(appFilePath.isNullOrBlank()){
          result.success(false)
          return
        }
        AppUtils.installApp(appFilePath)
        result.success(true)

      }
      "checkInstallApp" -> {
        //TODO 检测app是否安装，如果app已经安装，就删除app文件
      }
      "moveTaskToBack" -> {
        val intent = Intent(Intent.ACTION_MAIN)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.addCategory(Intent.CATEGORY_HOME)

      }
      "weChatPay" -> {
        val intent = Intent(Intent.ACTION_MAIN)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.addCategory(Intent.CATEGORY_HOME)

      }
      else ->{
        result.notImplemented()
      }
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }
}
